﻿@model cloudscribe.Core.Web.ViewModels.SiteUser.LoginViewModel
@using System.Collections.Generic
@using Microsoft.AspNetCore.Http
@using Microsoft.AspNetCore.Authentication
@using cloudscribe.Core.Models
@using cloudscribe.Core.Web
@using cloudscribe.Core.Web.Components
@using Microsoft.Extensions.Options
@using Microsoft.Extensions.Localization
@inject SiteContext Tenant
@inject IStringLocalizer<CloudscribeCore> sr
@inject SiteContext site
@inject IOptions<UIOptions>  uiOptionsAccessor
@{
    ViewData["BodyClass"] = "account-section login-page";
    ViewData["SocialFormat"] = sr["Log in using your {0} account"];
    ViewData["SocialHeading"] = sr["Use an existing account to log in"];
    var socialAuthoColumnWidth = 4;
    if (Model.DisableDbAuth) { socialAuthoColumnWidth = 12; }
    var uiOptions = uiOptionsAccessor.Value;
}
@section Meta {
    <meta name="robots" content="noindex" />
}
@if (!string.IsNullOrWhiteSpace(Model.RecaptchaSiteKey))
{
    <script src='https://www.google.com/recaptcha/api.js'></script>
}
@if (Model.UseInvisibleCaptcha)
{
    <script src="~/cr/js/cloudscribe-login-page.js"></script>
}
<h2>@ViewData["Title"]</h2>
@if (!string.IsNullOrWhiteSpace(Model.LoginInfoTop))
{
    <div class="row">
        <div class="col-md-12">
            @Html.Raw(Model.LoginInfoTop)
        </div>
    </div>
}
<div class="row">
    <div class="col-md-8">
        @if (!Model.DisableDbAuth)
        {
            <section id="loginForm">
                <form id="login-form" asp-controller="Account" asp-action="Login" asp-route-returnurl='@ViewData["ReturnUrl"]' method="post" class="form-horizontal" role="form" data-submit-once="true">
                    <div asp-validation-summary="All" class="text-danger"></div>
                    <div class="form-group">
                        @if (Model.UseEmailForLogin)
                        {
                            <label asp-for="UserName" class="col-md-2 control-label">@sr["Email or Username"]</label>
                        }
                        else
                        {
                            <label asp-for="UserName" class="col-md-2 control-label">@sr["Username"]</label>
                        }
                        <div class="col-md-10">
                            <div class="input-group">
                                <span class="input-group-addon"><i class="@uiOptions.IconUsername" aria-hidden="true"></i></span>
                                <input asp-for="UserName" class="form-control" />
                            </div>
                            <span asp-validation-for="UserName" class="text-danger"></span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label asp-for="Password" class="col-md-2 control-label">@sr["Password"]</label>
                        <div class="col-md-10">
                            <div class="input-group">
                                <span class="input-group-addon"><i class="@uiOptions.IconPassword" aria-hidden="true"></i></span>
                                <input asp-for="Password" class="form-control" />
                            </div>
                            <span asp-validation-for="Password" class="text-danger"></span>
                        </div>
                    </div>
                    @if (site.AllowPersistentLogin)
                    {
                        <div class="form-group">
                            <div class="col-md-offset-2 col-md-10">
                                <div class="checkbox">
                                    <label>
                                        <input asp-for="RememberMe" /> @sr["Remember Me"]
                                    </label>
                                </div>
                            </div>
                        </div>
                    }
                    @if (!string.IsNullOrWhiteSpace(Model.RecaptchaSiteKey) && !Model.UseInvisibleCaptcha)
                    {
                        <div class="form-group">
                            <div class="col-md-offset-2 col-md-10">
                                <div class="g-recaptcha" data-sitekey="@Model.RecaptchaSiteKey"></div>
                                @Html.ValidationMessage("recaptchaerror", new { @class = "text-danger" })
                            </div>
                        </div>
                    }
                    <div class="form-group">
                        <div class="col-md-offset-2 col-md-10">
                            @if (!string.IsNullOrWhiteSpace(Model.RecaptchaSiteKey) && Model.UseInvisibleCaptcha)
                            {
                                <button class="btn btn-default g-recaptcha" data-sitekey="@Model.RecaptchaSiteKey" data-callback='onCaptchaSubmit' data-enabled-text='@sr["log in"]' data-disabled-text='@sr["Working..."]'>@sr["Log in"]</button>
                            }
                            else
                            {
                                <button name="submit" type="submit" class="btn btn-default" data-enabled-text='@sr["Log in"]' data-disabled-text='@sr["Working..."]'>@sr["Log in"]</button>
                            }

                        </div>
                    </div>
                    @if (site.AllowNewRegistration)
                    {
                        <p>
                            <a asp-action="Register" asp-route-returnurl="@ViewData["ReturnUrl"]">@sr["Register as a new user?"]</a>
                        </p>
                    }
                    <p>
                        <a asp-action="ForgotPassword">@sr["Forgot your password?"]</a>
                    </p>
                </form>
            </section>
        }
    </div>
    <div class="col-md-@socialAuthoColumnWidth">
        @{
            var LoginProviders = Model.ExternalAuthenticationList.ToList();
            if (LoginProviders.Count > 0)
            {
                await Html.RenderPartialAsync("SocialLoginPartial", LoginProviders);
            }
        }
    </div>
</div>
@if (!string.IsNullOrWhiteSpace(Model.LoginInfoBottom))
{
    <div class="row">
        <div class="col-md-12">
            @Html.Raw(Model.LoginInfoBottom)
        </div>
    </div>
}
@section Scripts {
    @{await Html.RenderPartialAsync("_UnobtrusiveValidationScriptsPartial"); }
    <script src="~/cr/js/cloudscribe-validation-requiredwhen.min.js"></script>
    <script src="~/cr/js/jquery.validate.hooks.min.js"></script>
    <script src="~/cr/js/jqueryvaildation.submitonce-unobtrusive.min.js"></script>
}
